package com.wzk.travelspringboot.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzk.travelspringboot.dto.NoticeDTO;
import com.wzk.travelspringboot.dto.Result;
import com.wzk.travelspringboot.entity.SystemNotice;
import com.wzk.travelspringboot.service.SystemNoticeService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author wangzikang
 */
@Slf4j
@RestController
@RequestMapping("/admin/notice")
@RequiredArgsConstructor
@Validated
public class AdminSystemNoticeController {

    private final SystemNoticeService systemNoticeService;

    /**
     * 添加系统通知
     */
    @PostMapping("/add")
    public Result addSystemNotice(@RequestBody @Valid NoticeDTO dto) {
        return systemNoticeService.insertOne(dto);
    }

    /**
     * 删除系统通知
     */
    @DeleteMapping("/remove/{id}")
    public Result deleteSystemNotice(@PathVariable("id") @NotNull(message = "通知ID不能为空") Long id) {
        return systemNoticeService.deleteById(id);
    }

    /**
     * 查询所有系统通知
     */
    @GetMapping("/list")
    public Result listAllNotices() {
        return systemNoticeService.queryAll();
    }

    /**
     * 分页查询系统通知
     */
    @GetMapping("/page")
    public Result pageNotices(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize) {
        Page<SystemNotice> page = new Page<>(pageNum, pageSize);
        Page<SystemNotice> noticePage = systemNoticeService.page(page);
        return Result.success(noticePage);
    }

    /**
     * 根据ID查询系统通知详情
     */
    @GetMapping("/detail/{id}")
    public Result getNoticeDetail(@PathVariable("id") @NotNull(message = "通知ID不能为空") Long id) {
        SystemNotice notice = systemNoticeService.getById(id);
        if (notice == null) {
            return Result.failed("通知不存在");
        }
        return Result.success(notice);
    }

    /**
     * 更新系统通知
     */
    @PostMapping("/update")
    public Result updateNotice(@RequestBody @Valid NoticeDTO dto) {
        if (dto.getUserId() == null) {
            return Result.failed("用户ID不能为空");
        }
        // 创建SystemNotice对象
        SystemNotice notice = new SystemNotice();
        notice.setTitle(dto.getTitle());
        notice.setContent(dto.getContent());
        notice.setType(dto.getType());
        notice.setUserId(dto.getUserId());
        
        // 更新通知
        boolean success = systemNoticeService.updateById(notice);
        return success ? Result.success("更新通知成功") : Result.failed("更新通知失败");
    }

    /**
     * 根据类型查询系统通知
     */
    @GetMapping("/type/{type}")
    public Result getNoticesByType(@PathVariable("type") @NotNull(message = "通知类型不能为空") Integer type) {
        LambdaQueryWrapper<SystemNotice> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SystemNotice::getType, type);
        return Result.success(systemNoticeService.list(queryWrapper));
    }
} 